﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Marbles.Core.Connectors;
using System.Data;
using Marbles.Core.Content.Sites;

namespace Marbles.Core.Content.Links
{
    public class Link
    {

        public string SiteId { get { return _siteid; } }
        public string LinkId { get { return _linkid; } }
        public string Title { get { return _title; } set { _title = value; } }
        public string Url { get { return _link; } set { _link = value; } }
        public Uri Uri { get { return new Uri(_link); } }
        public string Description { get { return _description; } set { _description = value; } }
        public string ShortLinkUrl { get { return "Link.axd?id=" + LinkId; } }
        public DateTime LastAccess { get { return _LastAccess; } }
        public string LastAccessDate { get { return _LastAccess.Year > 1980 ? _LastAccess.ToString("dd/MM/yyyy HH:mm:ss") : ""; } }

        private DateTime _LastAccess = Marbles.Core.Connectors.DataConnection.MinimumDateTime;
        private string _siteid = string.Empty;
        private string _linkid = string.Empty;
        private string _title = string.Empty;
        private string _link = string.Empty;
        private string _description = string.Empty;

        public Link(string SiteId, string LinkId)
            : this()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("SELECT * FROM links WHERE siteid = @siteid AND linkid = @linkid");
            _siteid = SiteId;
            _linkid = LinkId;
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@linkid", _linkid);
            DataTable dt = dbc.FetchDataTable(cmd);

            if (dt.Rows.Count > 0)
            {
                _siteid = dt.Rows[0]["siteid"].ToString();
                _linkid = dt.Rows[0]["linkid"].ToString();
                _link = dt.Rows[0]["link"].ToString();
                _title = dt.Rows[0]["title"].ToString();
                _description = dt.Rows[0]["description"].ToString();
                _LastAccess = Convert.ToDateTime(dt.Rows[0]["lastaccess"]);
            }
        }

        public Link()
        {
            _siteid = string.Empty;
            _linkid = string.Empty;
            _title = string.Empty;
            _link = string.Empty;
            _description = string.Empty;
        }

        public Link(SiteDefinition Web, string LinkId)
            : this(Web.SiteId, LinkId)
        { }

        internal Link(DataRow row)
            : this()
        {
            _siteid = row["siteid"].ToString();
            _linkid = row["linkid"].ToString();
            _link = row["link"].ToString();
            _title = row["title"].ToString();
            _LastAccess = Convert.ToDateTime(row["lastaccess"]);
            _description = row["description"].ToString();
        }

        public static Link CreateNew(SiteDefinition Web)
        {
            return CreateNew(Web.SiteId);
        }

        public static Link CreateNew(string SiteId)
        {
            Link _link = new Link();
            _link._siteid = SiteId;
            _link._linkid = Guid.NewGuid().ToString();
            return _link;
        }

        public void Update()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE links SET title = @title, description = @description, link = @link WHERE siteid = @siteid AND linkid = @linkid");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@title", _title);
            cmd.AddParameter("@description", _description);
            cmd.AddParameter("@link", _link);
            cmd.AddParameter("@linkid", _linkid);
            dbc.ExecuteCommand(cmd);
        }

        public void Insert()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("INSERT INTO links (lastaccess,siteid, linkid, title, link, description) VALUES (@lastaccess, @siteid, @linkid, @title, @link, @description)");
            cmd.AddParameter("@lastaccess", new DateTime(1980, 1, 1, 0, 0, 0));
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@title", _title);
            cmd.AddParameter("@description", _description);
            cmd.AddParameter("@link", _link);
            cmd.AddParameter("@linkid", _linkid);
            dbc.ExecuteCommand(cmd);
        }

        public void Delete()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("DELETE FROM links WHERE siteid = @siteid AND linkid = @linkid");
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@linkid", _linkid);
            dbc.ExecuteCommand(cmd);
        }


        public void UpdateLastAccess()
        {
            DataConnection dbc = new DataConnection();
            Command cmd = new Command("UPDATE links SET lastaccess = @lastaccess WHERE siteid = @siteid AND linkid = @linkid");
            cmd.AddParameter("@lastaccess", DateTime.Now);
            cmd.AddParameter("@siteid", _siteid);
            cmd.AddParameter("@linkid", _linkid);
            dbc.ExecuteCommand(cmd);
        }
    }
}